package demo6;
/*

class Money implements Cloneable {
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable {
    public String name;
    public int age;

    public Money money = new Money();

    public Person(String name,int age) {
        this.name = name;
        this.age  = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.money = (Money)this.money.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("张三",10);
        Person person2 = (Person)person.clone();
        System.out.println(person2);

        System.out.println(person.money.m);
        System.out.println(person2.money.m);
        person.money.m=99.9;
        System.out.println(person.money.m);
        System.out.println(person2.money.m);
    }
}
*/

class Money implements Cloneable {
    public double m = 19.99;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Animal implements Cloneable{
    public String name;
    public int age;

    Money money = new Money();

    public Animal(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Animal tmp = (Animal)super.clone();
        tmp.money = (Money)this.money.clone();
        return tmp;
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Animal animal = new Animal("erha",32);
        Animal animal2 = (Animal)animal.clone();
        System.out.println(animal);
        System.out.println(animal2);
        System.out.println(animal.money.m);
        System.out.println(animal2.money.m);

        animal.money.m=99.99;

        System.out.println(animal.money.m);
        System.out.println(animal2.money.m);
    }
}

